package com.gsm.phimhanhdong;

import java.util.ArrayList;
import java.util.TreeSet;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class AlphabetAdapter extends BaseAdapter {

	public AlphabetAdapter(Context context) {
		// TODO Auto-generated constructor stub
		this.context = context;
		catalouges = new ArrayList<Catalouge>();
	}

	private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	ArrayList<Catalouge> catalouges;
	Context context;
	private static final int TYPE_ITEM = 0;
	private static final int TYPE_SEPARATOR = 1;
	private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
	private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();
	private OnCheckedChangeListener oncheckedchanglistenner;

	public OnCheckedChangeListener getOncheckedchanglistenner() {
		return oncheckedchanglistenner;
	}

	public void setOncheckedchanglistenner(
			OnCheckedChangeListener oncheckedchanglistenner) {
		this.oncheckedchanglistenner = oncheckedchanglistenner;
	}

	//
	public void addItem(final Catalouge item) {
		catalouges.add(item);
		notifyDataSetChanged();
	}

	public void addSeparatorItem(final Catalouge item) {
		catalouges.add(item);
		// save separator position
		mSeparatorsSet.add(catalouges.size() - 1);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	@Override
	public int getCount() {

		return catalouges.size();
	}

	@Override
	public Catalouge getItem(int position) {
		Log.e("test", "getItem");
		return catalouges.get(position);
	}

	@Override
	public long getItemId(int position) {
		Log.e("test", "getItemId");
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			int type = getItemViewType(position);
			switch (type) {
			case TYPE_ITEM:
				convertView = LayoutInflater.from(context).inflate(
						R.layout.item_menu, null);
				break;
			case TYPE_SEPARATOR:
				convertView = LayoutInflater.from(context).inflate(
						R.layout.headeralphabet, null);
				break;
			}
		}
		int type = getItemViewType(position);
		switch (type) {
		case TYPE_ITEM:
			TextView txt = (TextView) convertView.findViewById(R.id.catalouage);
			txt.setText(catalouges.get(position).title);
			convertView.setTag(catalouges.get(position).name);
			break;
		case TYPE_SEPARATOR:
			TextView txt1 = (TextView) convertView
					.findViewById(R.id.textSeparator);
			txt1.setText(catalouges.get(position).title);
			convertView.setEnabled(false);
			convertView.setFocusable(false);
			convertView.setFocusableInTouchMode(false);
			convertView.setClickable(false);
			convertView.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
			convertView.setOnClickListener(null);
			break;
		default:
			break;
		}

		return convertView;
	}

}
